//
//  ViewController.m
//  王宇先_12
//
//  Created by wyx on 16/3/24.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "ModleOfContact.h"
#import "JSONParser.h"
#import "CellOfNews.h"
#import "TableViewOfNews.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) TableViewOfNews *tableView;


@property (nonatomic, retain)NSMutableArray *arr;

@end

@implementation ViewController
#pragma mark - dealloc
-(void)dealloc{
    [_tableView release];
    [_arr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"新闻";
    [self createTableView];
    [self handleDate];
}
#pragma mark - Date
-(void)handleDate{
    
    id result = [JSONParser JSONParserWithFileName:@"news" type:@"txt"];
    
    NSArray *arrOfNews = [result objectForKey:@"news"];
    self.arr = [NSMutableArray array];
    //遍历数组
    for (NSDictionary *dic in arrOfNews) {
        ModleOfContact *modle = [[ModleOfContact alloc] init];
        [modle setValuesForKeysWithDictionary:dic];
        [self.arr addObject:modle];
        [modle release];
        
    }
}
#pragma mark - 创建 tableView
- (void)createTableView{
    self.tableView = [[TableViewOfNews alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    //指定代理人
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorColor = [UIColor redColor];
    //注册 cell
    [self.tableView registerClass:[CellOfNews class] forCellReuseIdentifier:@"pool"];
    [self.tableView release];
    
    
}
#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
    
}
#pragma mark - cell 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellOfNews *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    //通过 API赋值
    ModleOfContact *modle = [self.arr objectAtIndex:indexPath.row];
    [cell setCellContentValue:modle];
    //cell 选中风格
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    return cell;
}
#pragma mark - 返回每一行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ModleOfContact *modle = [self.arr objectAtIndex:indexPath.row];
    return [CellOfNews heightOfCellNews:modle.summary];
}
#pragma mark - other
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
